import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import nattyStorage from 'natty-storage';
import DB from '../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    searchText: false,
    searchAction:false,
    type:0,
    results:{},
    statusDes: [],
    statusValue:[]
  },

  onHandleChange:function(text){

  },

  onHandleSubmit:function(text){
    let type=0;
    let that=this;
    this.data.searchAction = update(this.data.searchAction, {$set: true});
    if(text.substr(0,4)=="SGXK"){
      type=1;
      text=text.substr(4,text.length);
      DB.WechatAPI.querysgxkjd({
        text:text
      }).then(
        function (content) {
          console.info(content);
          if(content.code=="200"){
            that.data.results = update(that.data.results, {$set: content});
            that.data.searchText = update(that.data.searchText, {$set: text});
            that.data.type = update(that.data.type, {$set: type});
            that.data.searchText = update(that.data.searchText, {$set: true});
            that.updateComponent();
            that.tzJinDuUi();
          }else{
            that.updateComponent();
          }

        },
        function (error) {
          console.log(error);
        }
      );
    }

    else if(text.substr(0,4)=="SGHT"){
      type=3;
      text=text.substr(4,text.length);
      DB.WechatAPI.querysghtbajd({
        text:text
      }).then(
        function (content) {
          console.info(content);
          if(content.code=="200"){
            that.data.results = update(that.data.results, {$set: content});
            that.data.searchText = update(that.data.searchText, {$set: text});
            that.data.type = update(that.data.type, {$set: type});
            that.data.searchText = update(that.data.searchText, {$set: true});
            that.updateComponent();
            that.tzJinDuUi();
          }else{
            that.updateComponent();
          }

        },
        function (error) {
          console.log(error);
        }
      );
    }

    else if(text.substr(0,4)=="XMBG"){
      type=2;
      text=text.substr(4,text.length);
      DB.WechatAPI.queryxmbrybgjd({
        text:text
      }).then(
        function (content) {
          console.info(content);
          if(content.code=="200"){
            that.data.results = update(that.data.results, {$set: content});
            that.data.searchText = update(that.data.searchText, {$set: text});
            that.data.type = update(that.data.type, {$set: type});
            that.data.searchText = update(that.data.searchText, {$set: true});
            that.updateComponent();
            that.tzJinDuUi();
          }else{
            that.updateComponent();
          }

        },
        function (error) {
          console.log(error);
        }
      );
    }
    else{
      this.data.searchText = update(this.data.searchText, {$set: false});
      this.updateComponent();
    }
  },

  onSaoyisao:function(){
    alert("扫一扫");
  },

  onGetStatusDes:function () {
    let that=this;
    DB.WechatAPI.getJdStatusDes().then(
      function (content) {
        console.info(content);
        that.data.statusDes = update(that.data.statusDes, {$set: content});
        that.updateComponent();
      },
      function (error) {
        console.log(error);
      }
    );
  },

  tzJinDuUi:function () {
    let that=this;
    let jinduUi=[];
    let tianjia=true;
    let tianjiajiezhi=false;
    let k=0;
    let tianjiaweizhi=0;
    for(let value of this.data.statusDes){
      let jindu={};
      if(value.status=="填报中"||value.status=="待审核"||
        value.status=="初审通过"
        ||value.status=="复审通过" ||value.status=="审核通过"){
        jindu.status=value.status;
        jindu.statusCode=parseInt(value.statusCode);
        let codeNumber=-1;
        if(null!=this.data.results.stateCode){
          codeNumber=parseInt(this.data.results.stateCode.substr(4,this.data.results.stateCode.length));
        }
        if(jindu.statusCode==codeNumber){
          tianjia=false;
          jindu.icon="process";
          jindu.description=this.data.results.stateTime;
        }
        else if(jindu.statusCode>codeNumber){
          if(!tianjiajiezhi){
            tianjiaweizhi=k;
            tianjiajiezhi=true;
          }
          jindu.icon="wait";
          jindu.description="";
        }
        else{
          jindu.icon="finish";
          jindu.description="";
        }
        jinduUi.push(jindu);
        k++;
      }
    }
    if(tianjia){
      jinduUi.splice(tianjiaweizhi, 0, {
        status:that.data.results.state,
        statusCode:that.data.results.stateCode,
        icon:"process",
        description:that.data.results.stateTime
      });
    }
    this.data.statusValue = update(this.data.statusValue, {$set: jinduUi});
    this.updateComponent();
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
